/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package contest.util;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 *
 * @author Admin
 */
public class LanguageBundle {

    static LanguageBundle languageBundle;
    Boolean isBuilt;
    ResourceBundle resource;

    private LanguageBundle() {
        this.isBuilt = false;
    }

    public void buildPath(String locationPath) throws MalformedURLException {

        if (!this.isBuilt && !locationPath.equals("")) {
            File file = new File(locationPath);

            if (file != null) {

                URL[] urls = {file.toURI().toURL()};
                ClassLoader loader = new URLClassLoader(urls);
                this.resource = ResourceBundle.getBundle(Constant.LANGUAGE, Locale.getDefault(), loader);
                this.isBuilt = true;

            }
        }

    }

    public static LanguageBundle getInstance() {
        if (LanguageBundle.languageBundle == null) {
            LanguageBundle.languageBundle = new LanguageBundle();
        }
        return LanguageBundle.languageBundle;
    }

    public Boolean isBuilt() {
        return this.isBuilt;
    }

    public void resetBuild(String locationPath) throws MalformedURLException {
        this.isBuilt = false;
        this.buildPath(locationPath);
    }

    public String get(String key) {
        if (this.resource != null && this.resource.getString(key) != null) {
            return this.resource.getString(key);
        }
        return "";
    }
}
